<?php if (isset($add)) echo "<h2>Thêm Tour</h2>"; else echo "<h2>Thay đổi thông tin tour</h2>";?>
<form name="edit_tour" action="?act=createOrModifyTour&tour_id=<?php echo (isset($tour_id)? $tour_id:null) ?>" method="POST" onsubmit="return validate_values()" enctype="multipart/form-data">
	<?php
		$tour = new Tour();
		if (!isset($add))
		  $tour->select($tour_id);
	?>
	<div class='row_field'>
    	<div class='label'>
			<label for="name">Tên tour(*):</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="text" id="tour_name" name="tour_name" value="<?php echo $tour->getname()?>">
        </div>
	</div>
    <div class='row_field'>
    	<div class='label'>
			<label>Loại tour:</label>
        </div>
        <div class='field'>
        	<div style='width:200px;float:left'>
            	<div style='float:left'>
					<input type='radio' name='tour_type' id='rad_type_1' <?php if ($tour->gettour_type() != "PERIODIC") echo "checked='checked'" ?> onclick="$('.txt_tour_date').show()" value="NORMAL" /> 
                </div>
                <div style='float:left'>
                	<label for="rad_type_1">Tour bình thường</label>
                </div>
            </div>
            <div style='width:200px;float:left'>
            	<div style='float:left'>
					<input type='radio' name='tour_type' id='rad_type_2' <?php if ($tour->gettour_type() == "PERIODIC") echo "checked='checked'" ?> onclick="$('.txt_tour_date').hide()" value="PERIODIC" /> 
                </div>
                <div style='float:left'>
                	<label for="rad_type_2">Tour định kỳ</label>
                </div>
            </div>
        </div>
	</div>
	<div class='row_field txt_tour_date'>
    	<div class='label'>
			<label for="start_date">Khởi hành(*):</label>
        </div>
        <div class='field'>
			<input type="text" id="start_date" name="start_date" class="inputbox date_picker" value="<?php echo $tour->getstart_date()?>">
        </div>
    </div>
    <div class='row_field txt_tour_date'>
    	<div class='label'>
			<label for="end_date">Kết thúc(*):</label>
        </div>
        <div class='field'>
			<input type="text" id="end_date" name="end_date" class="inputbox date_picker" onchange="verify_date()" value="<?php echo $tour->getend_date()?>">
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="number_day">Số ngày(*):</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="number" id="number_day" name="number_day" value="<?php echo $tour->getnumber_day()?>" onblur="must_be_integer(this)">
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="number_night">Số đêm(*):</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="number" id="number_night" name="number_night" value="<?php echo $tour->getnumber_night()?>" onblur="must_be_integer(this)">
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="price">Giá tour(*):</label>
        </div>
        <div class='field'>
		<input class='inputbox' type="number" id="price" name="price" value="<?php echo $tour->getprice()?>">
		<?php $currencies = get_currency_list() ?>
		<select id="currency" name="currency" class='inputbox'>
			<?php foreach ($currencies as $currency) {?>
			<option value="<?php echo $currency['id'] ?>" <?php 
				if ($currency['id'] == $tour->getcurrency())
					echo 'selected="selected"';
				elseif (isset($add) && $currency['currency_code'] == 'VND')
					echo 'selected="selected"' 
				?>><?php echo $currency['currency_code'] ?></option>
			<?php } ?>
		</select>
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="promotion">Khuyến mãi:</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="float" id="promotion" name="promotion" value="<?php echo $tour->getpromotion()?>">%
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="overview">Lược tả tour(*):</label>
        </div>
        <div class='field'>
			<textarea class='inputbox' name="overview" id="overview" rows="10" cols="50" ><?php echo $tour->getoverview()?></textarea>
        </div>
	</div>
    <div class='row_field'>
    	<div class='label'>
		    <label for="description">Miêu tả tour(*):</label>
        </div>
    </div>
	<div class='row_field'>
		<?php
		include("../fckeditor.php");
		$sBasePath = $_SERVER['PHP_SELF'] ;
		$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "admin" ) ) ;
		
		$oFCKeditor = new FCKeditor('description') ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Value		= $tour->getdescription();
		$oFCKeditor->Height		= 300;
		$oFCKeditor->Create() ;
		?>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="thumbnail">Hình đại diện:</label>
        </div>
		<div class='field'>
			<input class='inputbox' type="file" id="thumbnail" name="thumbnail">
        </div>
    </div>
    <div class='row_field'>
    	<?php
			$thm_pic = new Picture();
			$thumbnail = $tour->getthm_picture();
			if ($thumbnail) {
				$thm_pic->select($thumbnail);
			}
				
			echo "<div><img id='thm_picture' src='".$thm_pic->getPath()."'></div>"		
		?>
    </div>
    <div class='row_field'>
    	<div class='label'>
			<label for="thumbnail_name">Tên Hình:</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="text" id ="thumbnail_name" name="thumbnail_name" value="<?php echo $thm_pic->getdisplay_name()?>">
        </div>
	</div>
	<div class='row_field'>
    	<div class='label'>
			<label for="ads_picture">Hình quảng cáo:</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="file" id="ads_picture" name="ads_picture">
        </div>
    </div>
    <div class='row_field'>
    	<?php
			$ads_pic = new Picture();
			$ads_picture = $tour->getads_picture();
			if ($ads_picture) {
				$ads_pic->select($ads_picture);
			}
				
			echo "<div><img id='ads_picture' src='".$ads_pic->getPath()."'></div>"
		?>
    </div>
    <div class='row_field'>
		<div class='label'>
			<label for="ads_picture_name">Tên Hình:</label>
        </div>
        <div class='field'>
			<input class='inputbox' type="text" id ="ads_picture_name" name="ads_picture_name" value="<?php echo $ads_pic->getdisplay_name()?>">
		</div>
	</div>
    <div class='row_field'>
    	<div class='label'>
	    	<label for="categories_list">Chọn danh mục</label>
        </div>
        <div class='field'>
        	<?
			$tour_categories_old = '';
			$tour_categories = $tour->getAllTourCategories();
			$tour_category_ids = array();
			foreach($tour_categories as $tour_category)
			{
				$tour_categories_old .= $tour_category->getcategory_id().',';
				$tour_category_ids[] = $tour_category->getcategory_id();
			}
			$categories = Category::getAllCategories();
			load_checkbox_categories($categories, $tour_category_ids, $index)
			?>
            <input type='hidden' name='categories' id='categories' value='<?php echo $tour_categories_old;?>' />
            <div style='clear:both'></div>
        </div>
    </div>
	<div class='row_field'>
    	<div class='label'>
			<label for="active">Hiệu lực:</label>
        </div>
        <div class='field'>
			<input type="checkbox" id="active" name="active" <?php if ($tour->getactive() == 1 || isset($add)) echo 'checked="true"'?>>
        </div>
	</div>
	<div class='row_field'><p>(*) Thông tin bắt buộc</p></div>
	<div class='row_field'>
    	<input type='reset' value="Reset" class='button' />
		<input type="submit" value="Save" class="button" />
	</div>
	
</form>
<script type="text/javascript">
	function modify_tour_categories()
	{
		var tour_categories = "";
		$(".categories_list").each(function()
		{
			if ($(this).attr('checked')) {
				
				tour_categories += $(this).attr("category_id") + ",";
				
			}
		});
		$("#categories").val(tour_categories);
	}
	
	function verify_date() {
		start_date = $('#start_date').val();
		end_date = $('#end_date').val();
		if (end_date < start_date) {
			alert("Ngay Ket Thuc Khong The Truoc Ngay Khoi Hanh");
			$('#end_date').val(start_date);
		}		
	}
	
	
	function must_be_integer(element) {
		value = element.value;
		if (!IsInteger(value)) {
			alert('Truong nay phai co gia tri so');
			element.value = 0;
		}
	}
	
	
	function validate_values() {
		tour_name = $('#tour_name').val();
		start_date = $('#start_date').val();
		end_date = $('#end_date').val();
		number_day = $('#number_day').val();
		number_night = $('#number_night').val();
		depart = $('#depart').val();
		destination = $('#destination').val();
		price = $('#price').val();
		overview = $('#overview').val();
		description = $('#description').val();
		message = "Các mục sau đây là bắt buộc hoặc sai giá trị: \n";
		ok = true;
		if (tour_name == "") {
			message += "Tên tour\n";
			ok = false;
		}
		if (isNaN(parseInt(number_day)) || number_day <= 0) {
			message += "Số ngày\n";
			ok = false;
		}
		if (depart == "") {
			message += "Điểm khởi hành\n";
			ok = false;
		}
		if (destination == "") {
			message += "Điểm kết thúc\n";
			ok = false;
		}
		
		if (isNaN(parseInt(price)) || price <= 0) {
			message += "Giá tour\n";
			ok = false;
		}
		if (overview == "") {
			message += "Lược tả tour\n";
			ok = false;
		}
		if (!ok) alert(message);
		return ok;
	}
</script>

<?
function load_checkbox_categories($categories, $tour_category_ids, $index)
{
	foreach($categories as $category)
	{
	?>
		<div style='float:left;display:block;width:<?=435-$index*16?>px;padding-left:<?=$index*20?>px;padding-top:3px;padding-bottom:3px;'>
			<div style='float:left;padding-right:5px;'><input type='checkbox' name='category_<?php echo $category->getid();?>' class='categories_list' category_id="<?php echo $category->getid()?>" onClick="modify_tour_categories()" <?php echo (in_array($category->getid(),$tour_category_ids))?"checked='checked'":"";?> id='category_<?php echo $category->getid()?>' /></div>
			<div style='float:left;padding-right:10px;padding-top:3px;'><label for="category_<?php echo $category->getid();?>"><?php echo $category->getname()?></label></div>
			<div style='clear:both'></div>
            <?
			$category_children = Category::getAllCategories($category->getid());
			if ($category_children)
			{
				load_checkbox_categories($category_children, $tour_category_ids, $index+1);
			}
			?>
		</div>
	<?
	}
}


if ($tour->gettour_type() == "PERIODIC")
{
?>
<style>
	.txt_tour_date
	{
		display:none;
	}
</style>
<?
}
?>